import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@abp/components';
import {
  WeChatPaySettingEditDto,
  PaySettingsServiceProxy,
  AliPaySettingEditDto,
  GlobalAlipaySettingEditDto,
  PaySettingEditDto,
} from '@abp/service-proxies/service-proxies';
import { AppConsts } from '@core/abp/AppConsts';
import { AbpSessionService } from '@core/abp/session/abp-session.service';

@Component({
  templateUrl: './pay-settings.component.html',
  styleUrls: ['./pay-settings.component.less'],
})
export class PaySettingComponent extends AppComponentBase {
  weChatPay: WeChatPaySettingEditDto = new WeChatPaySettingEditDto();
  aliPay: AliPaySettingEditDto = new AliPaySettingEditDto();
  globalAliPay: GlobalAlipaySettingEditDto = new GlobalAlipaySettingEditDto();

  constructor(
    injector: Injector,
    private paySettingService: PaySettingsServiceProxy,
    private abpSessionService: AbpSessionService,
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.paySettingService.getAllSettings().subscribe(paySettiongResult => {
      this.weChatPay = paySettiongResult.weChatPay;
      this.aliPay = paySettiongResult.aliPay;
      this.globalAliPay = paySettiongResult.globalAliPay;

      if (this.weChatPay.payNotifyUrl === '') {
        let tenantId =
          this.abpSessionService.tenantId == null
            ? 0
            : this.abpSessionService.tenantId;
        this.weChatPay.payNotifyUrl =
          AppConsts.remoteServiceBaseUrl + '/PayNotify/' + tenantId + '/wechat';
      }
    });
  }

  submitForm() {
    let paySetting = new PaySettingEditDto();
    paySetting.weChatPay = this.weChatPay;
    paySetting.aliPay = this.aliPay;
    paySetting.globalAliPay = this.globalAliPay;
    this.paySettingService.updateAllSettings(paySetting).subscribe(result => {
      this.notify.success('保存成功！');
    });
  }
}
